package com.example.controller;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.example.common.Result;
import com.example.entity.*;
import com.example.service.OrderService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    //往订单表中添加一条订单信息=====================================
    @PostMapping("/addorder/{treenumber}")
    public Result addOrder(@PathVariable Integer treenumber,@RequestBody Integer treeId){
        orderService.addOrder(treenumber,treeId);
        return Result.success();
    }

    //商家往订单表中添加一条交换订单信息=====================================
    @PostMapping("/addbusinessorder/{treenumber}")
    public Result addBusinessOrder(@PathVariable Integer treenumber, @RequestBody String item){
        //生成订单编号
        String orderCode= RandomUtil.randomNumbers(14);
        orderService.addBusinessOrder(treenumber,item,orderCode);
        return Result.success();
    }
    //商家等价交换操作
    @PostMapping("/equalprice/{orderCode}")
    public Result equalPrice(@PathVariable String orderCode){
        orderService.equalPrice(orderCode);
        return Result.success();
    }
    //商家差额交换操作
    @PostMapping("/notequal/{orderCode}")
    public Result notEqual(@PathVariable String orderCode){
        orderService.notEqual(orderCode);
        return Result.success();
    }

    //发起方商家确认等价交换订单
    @PostMapping("/confirmequalorder/{orderCode}")
    public Result confirmEqualOrder(@PathVariable String orderCode){
        orderService.confirmEqualOrder(orderCode);
        return Result.success();
    }

    //发起方商家确认差额交换订单
    @PostMapping("/confirmnotequalorder/{orderCode}")
    public Result confirmNotEqualOrder(@PathVariable String orderCode){
        orderService.confirmNotEqualOrder(orderCode);
        return Result.success();
    }

    //接收方商家确认等价交换订单
    @PostMapping("/reconfirmequalorder/{orderCode}")
    public Result reconfirmOrder(@PathVariable String orderCode){
        orderService.reconfirmEqualOrder(orderCode);
        return Result.success();
    }
    //接收方商家确认差额交换订单
    @PostMapping("/reconfirmnotequalorder/{orderCode}")
    public Result reconfirmNotEqualOrder(@PathVariable String orderCode){
        orderService.reconfirmNotEqualOrder(orderCode);
        return Result.success();
    }


    //发起方收货 等价交换
    @PostMapping("/confirmequaltree/{orderCode}")
    public Result confirmEqualTree(@PathVariable String orderCode){
        orderService.confirmEqualTree(orderCode);
        return Result.success();
    }

    //发起方收货 差额交换
    @PostMapping("/confirmnotequaltree/{orderCode}")
    public Result confirmNotEqualTree(@PathVariable String orderCode){
//        orderService.confirmNotEqualTree(orderCode);
        return Result.success();
    }








    //商家交换订单取消操作=====================================
    @PostMapping("/cancelchange/{orderId}")
    public Result cancelChange(@PathVariable Integer orderId){
        orderService.cancelChange(orderId);
        return Result.success();
    }

    //商家交换订单确认收货操作=====================================
    @PostMapping("/confirmchangetree/{orderId}")
    public Result confirmChangeTree(@PathVariable Integer orderId,@RequestBody User user){
        orderService.confirmchangetree(orderId,user);
        return Result.success();
    }






    //检索用户购买订单信息并显示
    @PostMapping("/loadorder/{userId}")
    public Result loadOrder(@PathVariable Integer userId, @RequestBody Params params){
        PageInfo<OrderAndStatus> orderinfo = orderService.loadOrder(userId,params);
        return Result.success(orderinfo);
    }

    //检索商家交换订单并显示
    @PostMapping("/loadchangeorder")
    public Result loadChangeOrder(@RequestBody Params params){
        PageInfo<OrderAndStatus> orderinfo = orderService.loadChangeOrder(params);
        return Result.success(orderinfo);
    }



    //用户取消订单
    @PostMapping("/cancelorder/{orderId}")
    public Result cancelOrder(@PathVariable Integer orderId){
        orderService.cancelOrder(orderId);
        return Result.success();
    }

    //商家确认订单
    @PostMapping("/checkorder/{orderId}")
    public Result checkOrder(@PathVariable Integer orderId){
        orderService.checkOrder(orderId);
        return Result.success();
    }

    //商家确认取消订单
    @PostMapping("/checkcancel/{orderId}")
    public Result checkCancel(@PathVariable Integer orderId){
        orderService.checkCancel(orderId);
        return Result.success();
    }

    //用户确认收货
    @PostMapping("/confirmharvest/{orderId}")
    public Result confirmHarvest(@PathVariable Integer orderId){
        orderService.confirmHarvest(orderId);
        return Result.success();
    }

    //商家的用户订单页面查询订单
    @PostMapping("/selectuserorder")
    public Result selectUserOrder(@RequestBody Params params){
        PageInfo<OrderAndStatus> info = orderService.selectUserOrder(params);
        return Result.success(info);
    }

    //商家的交换订单页面查询订单
    @PostMapping("/selectchangeorder")
    public Result selectChangeOrder(@RequestBody Params params){
        PageInfo<OrderAndStatus> info = orderService.selectChangeOrder(params);
        return Result.success(info);
    }

    //用户售后服务评分写入数据库
    @PostMapping("/evaluate/{userId}")
    public Result evaluate(@RequestBody Evaluate evaluate, @PathVariable Integer userId){
        orderService.addEvaluate(evaluate,userId);
        return Result.success();
    }
    //售后服务用户建议部分
    @PostMapping("/usersuggestion/{userId}")
    public Result userSuggestion(@RequestBody Evaluate evaluate,@PathVariable Integer userId){
        orderService.userSuggestion(evaluate,userId);
        return Result.success();
    }

    //用户的我的反馈信息页面
    @PostMapping("/findevaluate/{userId}")
    public Result findeEvaluate(@RequestBody Params params,@PathVariable Integer userId){
        PageInfo<EvaluateAndStatus> info = orderService.findeEvaluate(params,userId);
        return Result.success(info);
    }

    //用户的我的反馈信息页面取消操作
    @PostMapping("/cancelevaluate/{evaluateId}")
    public Result cancelEvaluate(@PathVariable Integer evaluateId){
        orderService.cancelEvaluate(evaluateId);
        return Result.success();
    }

    //商家的用户反馈信息页面
    @PostMapping("/finduserevaluate/{userId}")
    public Result findUserEvaluate(@PathVariable Integer userId,@RequestBody Params params){
        PageInfo<EvaluateAndStatus> info = orderService.findUserEvaluate(params,userId);
        return Result.success(info);
    }

    //商家的用户反馈信息页面确认信息
    @PostMapping("/confirminfo/{evaluateId}")
    public Result confirmInfo(@PathVariable Integer evaluateId){
        orderService.confirmInfo(evaluateId);
        return Result.success();
    }

    //管理员的用户反馈信息页面
    @PostMapping("/findalluserevaluate")
    public Result findAllUserEvaluate(@RequestBody Params params){
        PageInfo<EvaluateAndStatus> info = orderService.findAllUserEvaluate(params);
        return Result.success(info);
    }

    //管理员提醒商家确认用户反馈信息
    @PostMapping("/remindbusiness/{evaluateId}")
    public Result remindBusiness(@PathVariable Integer evaluateId){
        orderService.remindBusiness(evaluateId);
        return Result.success();
    }

}
